<?php
	class ProductDao extends BaseDao {
		
		function getAllProducts() {
			parent::getConnect();
			$result = array();
			
			$rs = mysql_query("select id, thumbnail_id, name, catalog, publish_date, update_date, content, count from product");
			$record = mysql_fetch_object($rs);
			do {
				if ($record != null) {
					$result[count($result)] = $this->mappingProduct($record);
				}
				//$rsdata = mysql_query("select id, product_id, resource_id, position from product_resource");
				//$result[count($result)]->setProductResources(mappingProductResource($rsdata));
			} while ($record=mysql_fetch_object($rs));
// 			mysql_free_result($rsdata);
			mysql_free_result($rs);
			
			parent::releaseConnect();
			return $result;
		}
		
		function getTopProducts($limit) {
			parent::getConnect();
			$result = array();
				
			$rs = mysql_query("select id, thumbnail_id, name, catalog, publish_date, update_date, content, count from product limit $limit");
			$record = mysql_fetch_object($rs);
			do {
				if ($record != null) {
					$result[count($result)] = $this->mappingProduct($record);
				}
			} while ($record=mysql_fetch_object($rs));
			mysql_free_result($rs);
				
			parent::releaseConnect();
			return $result;
		}
		
		function getProducts($page) {
			parent::getConnect();
			$result = array();
			$start = ($page->getPageNo() - 1) * $page->getPageSize();
			$pageSize = $page->getPageSize();
			$rs = mysql_query("select id, thumbnail_id, name, catalog, publish_date, update_date, content, count from product limit $start, $pageSize");
			$record = mysql_fetch_object($rs);
			do {
				if ($record != null) {
					$result[count($result)] = $this->mappingProduct($record);
				}
			} while ($record=mysql_fetch_object($rs));
				
			$rs = mysql_query("select count(id) from product");
			$count = mysql_fetch_array($rs);
			$page->setData($result);
			$page->setTotalCount($count[0]);
				
			mysql_free_result($rs);
			parent::releaseConnect();
			return $page;
		}
		
		function getProduct($id) {
			parent::getConnect();
			$rs = mysql_query("select id, thumbnail_id, name, catalog, publish_date, update_date, content, count from product where id=$id");
			$record = mysql_fetch_object($rs);
			$product = $this->mappingProduct($record);
			$rsdata = mysql_query("select id, product_id, resource_id, position from product_resource where product_id=$id");
			$product->setProductResources($this->mappingProductResources($rsdata));
			mysql_free_result($rsdata);
			mysql_free_result($rs);
			parent::releaseConnect();
			return $product;
		}
		
		function addProduct($product) {
			parent::getConnect();
			$catalog = $product->getCatalog();
			$name = $product->getName();
			$content = $product->getContent();
			$count = $product->getCount();
			$rs = mysql_query("insert into product (name, catalog, content, count, publish_date, update_date) values ('$name', '$catalog', '$content', $count, now(), now())");
			$id = mysql_insert_id();
			$product->setId($id);
// 			parent::releaseConnect();
		}
		
		function addProductAndResource($products, $resources) {
			$resourceDao = new ResourceDao();
			$product = $this->addProduct($product);
			foreach($resources as $resource) {
				$resourceDao->addResource($resource);
				if (TRUE_CHAR == $resources->getIsThumbnail()) {
					$product->setThumbnailId($resource->getId());
				} else {
					$productResource = new ProductResource();
					$productResource->setProductId($product->getId());
					$productResource->setResourceId($resource->getId());
					$this->addProductResource($productResource);
				}
			} 
		}
		
		function deleteProduct($id) {
			parent::getConnect();
			$resourceDao = new ResourceDao();
			$product = $this->getProduct($id);
			if ($product->getThumbnailId() != null) {
				$resourceDao->deleteResource($product->getThumbnailId());
			}
			$this->deleteProductResourceByProductId($id);
			$prs = $product->getProductResources();
			foreach($prs as $pr) {
				$resourceDao->deleteResource($pr->getResourceId());
			}
			mysql_query("delete from product where id=$id");
			parent::releaseConnect();
		}
		
		function addProductResource($pr) {
			parent::getConnect();
			$productId = $pr->getProductId();
			$resourceId = $pr->getResourceId();
			$position = $pr->getPosition();
			$rs = mysql_query("insert into product_resource (product_id, resource_id, position) values ('$productId', '$resourceId', '$position')");
			$id = mysql_insert_id();
			$pr->setId($id);
			parent::releaseConnect();
		}
		
		function deleteProductResourceByProductId($id) {
			parent::getConnect();
			mysql_query("delete from product_resource where product_id=$id");
			parent::releaseConnect();
		}
		
		function updateProduct($product) {
			parent::getConnect();
			$catalog = $product->getCatalog();
			$name = $product->getName();
			$content = $product->getContent();
			$thumbnailId = $product->getThumbnailId();
			$count = $product->getCount();
			$id = $product->getId();
			$rs = mysql_query("update product set thumbnail_id=$thumbnailId, name='$name', count=$count, catalog='$catalog', content='$content' where id=$id");
			parent::releaseConnect();
		}
		
		private function mappingProductResources($rsdata) {
			$record = mysql_fetch_object($rsdata);
			$result = array();
			do {
				if ($record != null) {
					$result[count($result)] = $this->mappingProductResource($record);
				}
			} while ($record = mysql_fetch_object($rsdata));
			return $result;
		}
		
		private function mappingProductResource($record) {
			$pr = new ProductResource();
			$pr->setId($record->id);
			$pr->setPosition($record->position);
			$pr->setProductId($record->product_id);
			$pr->setResourceId($record->resource_id);
			return $pr;
		}
		
		private function mappingProduct($record) {
			$product = new Product();
			$product->setId($record->id);
			$product->setCatalog($record->catalog);
			$product->setContent($record->content);
			$product->setCount($record->count);
			$product->setName($record->name);
			$product->setPublishDate($record->publish_date);
			$product->setThumbnailId($record->thumbnail_id);
			$product->setUpdateDate($record->update_date);
			return $product;
		}
		
	}
?>